package com.coffee.daili.service.impl;

import com.coffee.admin.service.merchant.bean.Merchant;
import com.coffee.admin.service.merchant.dao.MerchantDao;
import com.coffee.admin.service.supplier.bean.Supplier;
import com.coffee.admin.service.supplier.dao.SupplierDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.DailiMerchantRoleEnum;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import com.coffee.daili.api.query.DailiMerchantQuery;
import com.coffee.daili.api.query.DailiMerchantSkuQuery;
import com.coffee.daili.api.req.DailiMerchantSkuListReq;
import com.coffee.daili.api.result.DailiMerchantSkuResult;
import com.coffee.daili.api.service.DailiMerchantSkuService;
import com.coffee.daili.service.bean.DailiMerchant;
import com.coffee.daili.service.bean.DailiMerchantSku;
import com.coffee.daili.service.dao.DailiMerchantDao;
import com.coffee.daili.service.dao.DailiMerchantSkuDao;
import com.coffee.sku.api.sku.query.RegionalSkuSalesInformationQuery;
import com.coffee.sku.api.sku.query.SkuSalesInformationQuery;
import com.coffee.sku.api.sku.result.MerchantSkuListResult;
import com.coffee.sku.service.sku.bean.RegionalSkuSalesInformation;
import com.coffee.sku.service.sku.bean.SkuCategory;
import com.coffee.sku.service.sku.bean.SkuSalesInformation;
import com.coffee.sku.service.sku.dao.RegionalSkuSalesInformationDao;
import com.coffee.sku.service.sku.dao.SkuCategoryDao;
import com.coffee.sku.service.sku.dao.SkuSalesInformationDao;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;


@DubboService(dynamic = true)
public class DailiMerchantSkuServiceImpl implements DailiMerchantSkuService {
    @Resource
    private DailiMerchantSkuDao dailiMerchantSkuDao;
    @Resource
    private SkuCategoryDao skuCategoryDao;
    @Resource
    private SkuSalesInformationDao skuSalesInformationDao;
    @Resource
    private MerchantDao merchantDao;
    @Resource
    private DailiMerchantDao dailiMerchantDao;
    @Resource
    private SupplierDao supplierDao;
    @Resource
    private RegionalSkuSalesInformationDao regionalSkuSalesInformationDao;

    @Override
    public Page<DailiMerchantSkuResult> getPager(DailiMerchantSkuQuery param, PagerInfo pr) {
        Page<DailiMerchantSkuResult> pager = new Page<DailiMerchantSkuResult>(pr.getStart(), pr.getPageSize());
        Integer count = dailiMerchantSkuDao.count(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<DailiMerchantSku> list = dailiMerchantSkuDao.page(param, pr.getStart(), pr.getPageSize());
            List<DailiMerchantSkuResult> dailiMerchantSkuResults = DTOUtils.convertList(list, DailiMerchantSkuResult.class);
            for (DailiMerchantSkuResult dailiMerchantSkuResult : dailiMerchantSkuResults) {
                if (dailiMerchantSkuResult.getCategoryId() != null) {
                    SkuCategory skuCategory = skuCategoryDao.findById(dailiMerchantSkuResult.getCategoryId());
                    if (skuCategory != null) {
                        dailiMerchantSkuResult.setCategoryName(skuCategory.getName());
                    }
                }
                if (dailiMerchantSkuResult.getSupplierId() != null) {
                    Supplier supplier = supplierDao.findById(dailiMerchantSkuResult.getSupplierId());
                    if (supplier != null) {
                        dailiMerchantSkuResult.setSupplierName(supplier.getName());
                    }
                }
            }
            pager.setData(dailiMerchantSkuResults);
        }
        return pager;
    }

    @Override
    public Integer count(DailiMerchantSkuQuery param) {
        Integer count = dailiMerchantSkuDao.count(param);
        return count;
    }

    @Override
    public DailiMerchantSkuResult getDetail(Integer id) {
        DailiMerchantSku item = dailiMerchantSkuDao.findById(id);
        if (item == null) {
            return null;
        }
        DailiMerchantSkuResult result = new DailiMerchantSkuResult();
        BeanUtilExt.copyProperties(result, item);

        return result;
    }

    @Override
    public List<DailiMerchantSkuResult> getList(DailiMerchantSkuQuery param, PagerInfo pr) {
        List<DailiMerchantSku> list = dailiMerchantSkuDao.page(param, pr.getStart(), pr.getPageSize());
        return DTOUtils.convertList(list, DailiMerchantSkuResult.class);
    }

    @Override
    public List<DailiMerchantSkuResult> getList(DailiMerchantSkuQuery param) {
        List<DailiMerchantSku> list = dailiMerchantSkuDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, DailiMerchantSkuResult.class);
    }

    @Override
    public List<DailiMerchantSkuResult> getList() {
        DailiMerchantSkuQuery param = new DailiMerchantSkuQuery();
        List<DailiMerchantSku> list = dailiMerchantSkuDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, DailiMerchantSkuResult.class);
    }


    @Override
    public void delete(Integer id) {
        dailiMerchantSkuDao.deleteById(id);
    }

    @Override
    public void addOrUpdate(DailiMerchantSkuResult result) throws BusinessException {
        DailiMerchantSku item = new DailiMerchantSku();
        BeanUtilExt.copyProperties(item, result);
        if (item.getId() == null) {
            item.setCreateTime(new Date());
            dailiMerchantSkuDao.insert(item);
        } else {
            DailiMerchantSku tmp = dailiMerchantSkuDao.findById(item.getId());
            if (tmp == null) {
                throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
            }
            dailiMerchantSkuDao.updateById(item);
        }
    }

    @Override
    public Map<Integer, DailiMerchantSkuResult> getItemMap(Set<Integer> idSet) {
        Map<Integer, DailiMerchantSkuResult> map = new HashMap<Integer, DailiMerchantSkuResult>();
        List<DailiMerchantSku> list = dailiMerchantSkuDao.findByIds(StringUtil.setToList(idSet));
        List<DailiMerchantSkuResult> resultList = DTOUtils.convertList(list, DailiMerchantSkuResult.class);
        for (DailiMerchantSkuResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Map<Integer, DailiMerchantSkuResult> getItemMap() {
        Map<Integer, DailiMerchantSkuResult> map = new HashMap<Integer, DailiMerchantSkuResult>();
        DailiMerchantSkuQuery param = new DailiMerchantSkuQuery();
        List<DailiMerchantSku> list = dailiMerchantSkuDao.page(param, 0, Integer.MAX_VALUE);
        List<DailiMerchantSkuResult> resultList = DTOUtils.convertList(list, DailiMerchantSkuResult.class);
        for (DailiMerchantSkuResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Page<MerchantSkuListResult> findMerchantSkuList(DailiMerchantSkuListReq param, PagerInfo pr) {
        Merchant merchant = merchantDao.findById(param.getMerchantId());
        DailiMerchantQuery daliMerchantQuery = new DailiMerchantQuery();
        daliMerchantQuery.setMobile(merchant.getMobile());
        DailiMerchant daliMerchant = dailiMerchantDao.findFirst(daliMerchantQuery);
        if (daliMerchant == null) {
            return null;
        }
        Integer dailiMerchantId = daliMerchant.getId();
        if (DailiMerchantRoleEnum.DISTRIBUTOR.getCode().equals(daliMerchant.getRole())) {
            dailiMerchantId = daliMerchant.getPid();
        }
        Page<MerchantSkuListResult> pager = new Page<MerchantSkuListResult>(pr.getStart(), pr.getPageSize());
        param.setDailiMerchantId(dailiMerchantId);
        Integer count = dailiMerchantSkuDao.dailiMerchantSkuCount(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<MerchantSkuListResult> merchantSkuListReps = dailiMerchantSkuDao.getMerchantSkuListResultpage(param, pr.getStart(), pr.getPageSize());
            for (MerchantSkuListResult merchantSkuListRep : merchantSkuListReps) {
                SkuSalesInformationQuery skuSalesInformationQuery = new SkuSalesInformationQuery();
                skuSalesInformationQuery.setSkuId(merchantSkuListRep.getId());
                skuSalesInformationQuery.setIsDelete("N");
                List<SkuSalesInformation> list = skuSalesInformationDao.list(skuSalesInformationQuery);
                if (list.size() > 0) {
                    SkuSalesInformation skuSalesInformation = list.get(0);
                    merchantSkuListRep.setPrice(skuSalesInformation.getPrice());
                    merchantSkuListRep.setFengyongAmount(new BigDecimal(skuSalesInformation.getPrice() + "").multiply(new BigDecimal(merchantSkuListRep.getProportion() + "")).setScale(2, RoundingMode.HALF_EVEN).doubleValue());
                }
                if (param.getCityId() != null) {
                    RegionalSkuSalesInformationQuery regionalSkuSalesInformationQuery = new RegionalSkuSalesInformationQuery();
                    regionalSkuSalesInformationQuery.setGtRegionId(param.getCityId());
                    regionalSkuSalesInformationQuery.setSkuId(merchantSkuListRep.getId());
                    RegionalSkuSalesInformation regionalSkuSalesInformation = regionalSkuSalesInformationDao.findFirst(regionalSkuSalesInformationQuery);
                    if (regionalSkuSalesInformation != null) {
                        merchantSkuListRep.setSkuSalesInformationId(regionalSkuSalesInformation.getSkuSalesInformationId());
                        merchantSkuListRep.setPrice(regionalSkuSalesInformation.getRegionalPrice());
                        if (regionalSkuSalesInformation.getProportion() != null) {
                            merchantSkuListRep.setFengyongAmount(new BigDecimal(regionalSkuSalesInformation.getRegionalPrice() + "").multiply(new BigDecimal(regionalSkuSalesInformation.getProportion() + "")).setScale(2, RoundingMode.HALF_EVEN).doubleValue());
                        } else {
                            merchantSkuListRep.setFengyongAmount(new BigDecimal(merchantSkuListRep.getPrice() + "").multiply(new BigDecimal(merchantSkuListRep.getProportion() + "")).setScale(2, RoundingMode.HALF_EVEN).doubleValue());
                        }
                    }
                }
            }
            pager.setData(merchantSkuListReps);
        }
        return pager;
    }


}
